﻿using AnlianCommon.Common.Domains.UserPermissions;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.Personnels
{
    public class PersonnelBackFillHander : IRequestHandler<PersonnelBackFillCommand, Result>
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        private readonly IMapper _mapper;
        private readonly IBaseRepository<Personnel> _personnelRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="personnelRepository"></param>
        public PersonnelBackFillHander(IMapper mapper, IBaseRepository<Personnel> personnelRepository)
        {
            _mapper = mapper;
            _personnelRepository = personnelRepository;
        }
        /// <summary>
        /// 处理命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(PersonnelBackFillCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var personnel = await(await _personnelRepository.GetAll()).Where(c => c.PersId == request.PersId).FirstOrDefaultAsync();
                if (personnel != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = personnel;
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
